<?php

namespace console\db;

use yii\db\Schema;

/**
 * SchemaBuilderTrait contains shortcut methods to create instances of [[ColumnSchemaBuilder]].
 *
 * @author emhome <emhome@163.com>
 * @since 2.0.6
 */
trait SchemaBuilderTrait {

    /**
     * Creates a tinyint column.
     * @param int $length column size definition i.e. the maximum string length.
     * This parameter will be ignored if not supported by the DBMS.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function tinyint($length = null) {
        if ($length != null) {
            return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinyint', $length);
        }
        return $this->getDb()->getSchema()->createColumnSchemaBuilder(\yii\db\Schema::TYPE_BOOLEAN);
    }

    /**
     * Creates a tinyint column replace boolean.
     * @param int $length column size definition i.e. the maximum string length.
     * This parameter will be ignored if not supported by the DBMS.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function booleanInt() {
        return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinyint', 1);
    }

    /**
     * Creates a longtext column.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function longtext() {
        return $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext');
    }

    /**
     * Creates a enum column.
     * @param array $items column data definition.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function enum($items = []) {
        return $this->getDb()->getSchema()->createColumnSchemaBuilder("enum('" . implode("','", $items) . "')");
    }

    /**
     * Creates a unix timestamp column.
     * @param int $length column size definition i.e. the maximum string length.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function unixTimestamp($length = 10) {
        return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_INTEGER, $length)->unsigned()->notNull()->defaultValue(0);
    }

    /**
     * Creates a geometry column.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function geometry() {
        return $this->getDb()->getSchema()->createColumnSchemaBuilder('geometry');
    }

    /**
     * Creates a geometryHash column.
     * @param int $length column size definition i.e. the maximum string length.
     * @return ColumnSchemaBuilder the column instance which can be further customized.
     * @since 2.0.8
     */
    public function geometryHash($from, $size = 8, $length = 20) {
        $sql = "GENERATED ALWAYS AS (st_geohash(`$from`, $size)) VIRTUAL";
        return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, $length)->append($sql);
    }

}
